package com.herench.bunch.peis.convert;


import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.*;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.group.RTB_K13_ROW_DEFINITION;
import ca.uhn.hl7v2.model.v24.message.*;
import ca.uhn.hl7v2.model.v24.segment.MSA;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.QRD;
import ca.uhn.hl7v2.model.v24.segment.RDT;
import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
import ca.uhn.hl7v2.parser.ParserConfiguration;
import ca.uhn.hl7v2.parser.PipeParser;
import com.alibaba.fastjson.JSON;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.aspectj.weaver.bcel.ClassPathManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.net.URLDecoder;
import java.util.*;

public class PeisApplicationConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> toMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> resMap = new HashMap<String, Object>();
        String hl7 = requestMessage.getOriginalMsg();
        logger.info("pacs application: " + hl7);
        resMap.put("msg",hl7);
        return resMap;
    }

    @Override
    Map<String, Object> doToMessage(RequestMessage requestMessage) throws Exception {
        return null;
    }


    @Override
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        String res = URLDecoder.decode((String) message,"utf-8");
        RTB_K13 hl7Message;
        HapiContext context = new DefaultHapiContext();
        PipeParser pipeParser = context.getPipeParser();
        ParserConfiguration parserConfiguration = context.getParserConfiguration();
        parserConfiguration.setValidating(false);
        hl7Message = (RTB_K13)pipeParser.parse(res);
        return hl7Message;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QRY_Q01 qry = new QRY_Q01();
        qry.initQuickstart("QRY_Q01", "Q01", "A");
        MSH msh = qry.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("300020");
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        QRD qrd = qry.getQRD();

        int index = 0;
        qrd.insertQrd8_WhoSubjectFilter(index++).getXcn16_NameContext().getCe2_Text().setValue("4246178257441441008");
        qrd.insertQrd8_WhoSubjectFilter(index++).getXcn16_NameContext().getCe2_Text().setValue("001");
        qrd.insertQrd8_WhoSubjectFilter(index++).getXcn16_NameContext().getCe2_Text().setValue("1");
        qrd.insertQrd8_WhoSubjectFilter(index++).getXcn16_NameContext().getCe2_Text().setValue("010201");

        return qry;

    }
}
